package com.ceadeal.admin.service.article;

import com.ceadeal.admin.common.datatable.DataTableOutput;
import com.ceadeal.admin.entity.Article;
import com.ceadeal.admin.mapper.article.ArticleCategoryMapper;
import com.ceadeal.admin.mapper.article.ArticleMapper;
import com.ceadeal.admin.service.BaseService;
import com.ceadeal.admin.vo.param.SearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 文章
 *
 * @author Westin Yang
 * @date 2017-04-03 20:37:33
 */
@Service
@Transactional
public class ArticleService extends BaseService<Article> {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    /**
     * 功能描述: 分页查询
     * @param
     * @return
     * @throws
     *
     * @author Yzw
     * @date 2017-03-08 10:37:02
     */
    @Transactional(readOnly = true)
    public DataTableOutput findByPaging(SearchParams sp) {
        List<Article> list = this.articleMapper.findListByPaging(sp);
        int count = this.articleMapper.getCountByPaging(sp);
        // 封装并返回
        return new DataTableOutput<>(sp.getDraw(), count, list);
    }

    public void saveHandler(Article article) {
        Date currDate = new Date();
        article.setArticleCategoryId(1L);
        article.setContentType(Article.CONTENT_TYPE_MARKDOWN);

        if (article.getId() == null) {
            article.setCreateDate(currDate);
            this.save(article);
        } else {
            article.setUpdateDate(currDate);
            this.update(article);
        }
    }
}
